import os, time

def processf1() -> int:
    time_start = time.time()
    os.system("fftns --quiet ../CovID19new.fasta > ../ans1.txt")
    time_end = time.time()
    print("FFTNS use time: ", time_end - time_start)
    g = open("../ans1.txt")
    cnt = 0
    for line in g:
        if line[0] in "tcag-":
            #print(line)
            for i in line:
                if i == '-': cnt += 1
    g.close()
    return cnt

def processf2() -> int:
    time_start = time.time()
    os.system("python3 kband.py --file1 ../CovID19new.fasta --file2 ../CovID19new2.fasta > ../ans2.txt --band 30")
    time_end = time.time()
    print("KBand use time: ", time_end - time_start)
    g = open("../ans2.txt")
    cnt = 0
    for line in g:
        if line[1] in "TCAG-":
            for i in line:
                if i == '-': cnt += 1
    g.close()
    return cnt

def writedata(l, s1, s2, save, offset):
    '''if save[0] - l >= 0:
    else: 
        s1 = s1[0 : save[0] + l]
        s2 = s2[0 : save[0] + l]
    ''' 
    s1 = s1[0 : len(s1) - 200] * 100
    s2 = s2 * 100
    news1, news2 = '', ''
    ok = False
    for i in range(len(s1)):
        if s1[i] == s2[i]:
            news1 = news1 + s1[i]
            news2 = news2 + s2[i]
        if ok: continue
        elif s1[i] != s2[i] and s1[i + 1] != s2[i + 1]:
            news1 = news1 + s1[i] + s1[i + 1]
            news2 = news2 + s2[i] + s2[i + 1]
            ok = True
    if news1 == news2: raise TypeError
    s1, s2 = news1, news2
    g = open("../CovID19new.fasta", 'w')
    g.writelines(info1 + '\n' + s1 + '\n' + info2 + '\n' + s2)
    g.close()
    g = open("../CovID19new2.fasta", 'w')
    g.writelines(info2 + '\n' + s2)
    g.close()

if __name__ == '__main__':
    f = open("../CovID19.fasta")
    cnt = 0
    s1 = s2 = ''
    info1 = info2 = ''
    for line in f:
        cnt += 1
        if cnt == 2: s1 = line.strip('\n\r')
        elif cnt == 4: s2 = line.strip('\n\r')
        elif cnt == 1: info1 = line.strip('\n\r')
        elif cnt == 3: info2 = line.strip('\n\r')
    offset = 7
    warning = 0
    place = []
    save = []
    for i in range(len(s2)):
        if s1[offset + i] != s2[i]: 
            warning += 1
            place.append(i)
            print("Offset = " + str(i), s1[offset + i], s2[i])
            if warning >= 2:
                save = place
        else: 
            place = []
            warning = 0
    print(save)
    l, r = 300, (len(s2) - save[0]) >> 1
    
    for mid in range(l, r + 1):
    #while l < r:
        #mid = (l + r) >> 1
        writedata(mid, s1, s2, save, offset)
        f1 = processf1()
        f2 = processf2()
        print("mid, fftnsgetspace, kbandgetspace =", mid, f1, f2)
        #if f2 - offset < f1:
        #    r = mid - 1
        #else: l = mid + 1
        '''
    #writedata(27, s1, s2, save, offset)
    f1 = processf1()
    f2 = processf2()
    print("mid, fftnsgetspace, kbandgetspace =", 27, f1, f2)
    '''
    
    